# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _
from odoo.exceptions import UserError

import datetime



import logging
_logger = logging.getLogger(__name__)


class CrEmpStaff(models.Model):

    _name = "cr.emp.staff"
    _description = "人员"

    card_id = fields.Char(string='工号')
    name = fields.Char(string='姓名')
    sex = fields.Selection([
        ('man', '男'),
        ('woman', '女')], string= '性别')
    ws_id = fields.Many2one('cr.emp.workshop',string='所属车间',related='team_id.ws_id',store=True)
    team_id = fields.Many2one("cr.emp.team",string='班组')
    post_id = fields.Many2one("cr.emp.post",string='岗位')
    birthday = fields.Char(string='出生日期')
    photo_url = fields.Char(string='照片')

    is_tolerate = fields.Boolean(string='是否容错')
    is_correct = fields.Boolean(string='是否纠错')
    is_warning = fields.Boolean(string='是否预警')
    is_lose = fields.Boolean(string='是否失信')
    observe_start_time=fields.Datetime(string='纠错起')
    observe_end_time=fields.Datetime(string='纠错止')
    observe_time = fields.Char(string='观察期止', compute='_compute_observe_time', store=True)

    @api.depends('observe_start_time', 'observe_end_time')
    def _compute_observe_time(self):
        # lqy 观察期字符'
        for record in self:
            if record.observe_start_time and record.observe_end_time:
                record.observe_time = record.observe_start_time.strftime(
                    '%Y-%m-%d') + '至' + record.observe_end_time.strftime(
                    '%Y-%m-%d')
    #
    @api.model
    def create(self,vals):
        now=datetime.datetime.now()
        staff=super(CrEmpStaff, self).create(vals)
        promise=self.env['cr.emp.promise'].create({'staff_id':staff.id})
        score=self.env['cr.emp.score'].create({
            'time': now,
            'staff_id': staff.id,
            'self_num': 0,
            'other_num': 0,
            'check_num': 0,
        })
        warning=self.env['cr.emp.warning'].create({'time': now,
'staff_id':staff.id})
        inspire=self.env['cr.emp.inspire'].create({
            'score_id': int(score.id),
            'staff_id': staff.id,
            'year': now.year,
            'month': now.month
        })
        return staff

    @api.model
    def unlink(self):
        now = datetime.datetime.now()
        ids = self.ids
        for staff_id in ids:
            promise = self.env['cr.emp.promise'].search([('staff_id','=', staff_id)]).unlink()
            pledge = self.env['cr.emp.pledge'].search([('staff_id','=', staff_id)]).unlink()
            score = self.env['cr.emp.score'].search([('staff_id','=', staff_id)]).unlink()
            score_item = self.env['cr.emp.score.item'].search([('staff_id','=', staff_id)]).unlink()
            warning = self.env['cr.emp.warning'].search([('staff_id','=', staff_id)]).unlink()
            warning_item = self.env['cr.emp.warning.item'].search([('staff_id','=', staff_id)]).unlink()
            tolerate = self.env['cr.emp.tolerate'].search([('staff_id','=', staff_id)]).unlink()
            correct = self.env['cr.emp.correct'].search([('staff_id','=', staff_id)]).unlink()
            help = self.env['cr.emp.help'].search([('staff_id','=', staff_id)]).unlink()
            lose_item = self.env['cr.emp.lose.item'].search([('staff_id','=', staff_id)]).unlink()
            inspire = self.env['cr.emp.inspire'].search([('staff_id','=', staff_id)]).unlink()
        staff=super(CrEmpStaff, self).unlink()
        return staff



    def init_users(self):
        self.env['res.users'].sudo().search([]).write({'cr_emp_type':'admin'})
        list1 = [('0302', '杨子虹', 'station', 0, '111111'), ('1186', '古建忠', 'workshop', 8, '111111'),
                 ('2418', '张小军', 'station', 0, '111111'), ('2015', '栾哲', 'workshop', 7, '111111'),
                 ('2466', '李万春', 'station', 0, '111111'), ('0297', '周金生', 'workshop', 8, '111111'),
                 ('1619', '李青', 'workshop', 18, '111111'), ('6387', '刘聚军', 'workshop', 15, '111111'),
                 ('6001', '王震宇', 'workshop', 15, '111111'), ('1621', '宋庆超', 'workshop', 7, '111111'),
                 ('6567', '胡小平', 'workshop', 13, '111111'), ('6315', '郝文强', 'workshop', 12, '111111'),
                 ('1089', '巩海燕', 'workshop', 7, '111111'), ('1603', '朱海新', 'workshop', 8, '111111'),
                 ('0173', '任玉军', 'workshop', 9, '111111'), ('1370', '刘东海', 'workshop', 6, '111111'),
                 ('6532', '郭金民', 'workshop', 15, '111111'), ('2320', '郝战伟', 'station', 0, '111111'),
                 ('1122', '刘双青', 'station', 0, '111111'), ('0922', '宋和平', 'station', 0, '111111'),
                 ('2196', '张贵祥', 'station', 0, '111111'), ('2430', '朱德明', 'station', 0, '111111'),
                 ('1558', '张延波', 'station', 0, '111111'), ('1762', '刘志强', 'admin', 0, '111111'),
                 ('6568', '王洪津', 'workshop', 16, '111111'), ('1472', '毛永刚', 'workshop', 12, '111111'),
                 ('6019', '贾志洞', 'workshop', 17, '111111'), ('0284', '赵国华', 'workshop', 8, '111111'),
                 ('0779', '戴超', 'workshop', 15, '111111'), ('0106', '党红强', 'workshop', 15, '111111'),
                 ('6547', '李立军', 'workshop', 7, '111111'), ('1606', '岳燕红', 'admin', 0, '111111'),
                 ('1602', '朱杰', 'workshop', 1, '111111'), ('1598', '刘建东', 'workshop', 15, '111111'),
                 ('6252', '赵军文', 'workshop', 17, '111111'), ('6531', '李建新', 'workshop', 17, '111111'),
                 ('1902', '奚智勇', 'workshop', 12, '111111'), ('6405', '石旭辉', 'workshop', 12, '111111'),
                 ('6555', '张志梅', 'workshop', 12, '111111'), ('1451', '张军', 'workshop', 13, '111111'),
                 ('7726', '马志远', 'workshop', 13, '111111'), ('6540', '玉佩', 'workshop', 18, '111111'),
                 ('6546', '李建强', 'workshop', 16, '111111'), ('6545', '宋继春', 'workshop', 16, '111111'),
                 ('6285', '郝永风', 'workshop', 14, '111111'), ('1583', '李志刚', 'workshop', 14, '111111'),
                 ('1999', '周晓', 'workshop', 14, '111111'), ('6543', '杨金印', 'workshop', 9, '111111'),
                 ('1819', '李建华', 'workshop', 9, '111111'), ('1604', '张永清', 'workshop', 9, '111111'),
                 ('1319', '王献军', 'workshop', 10, '111111'), ('6544', '胡涛', 'workshop', 1, '111111'),
                 ('0859', '王栋', 'workshop', 1, '111111'), ('1510', '王宪军', 'workshop', 1, '111111'),
                 ('1993', '王旭光', 'workshop', 1, '111111'), ('6273', '徐磊', 'workshop', 14, '111111'),
                 ('1957', '李刚', 'workshop', 6, '111111'), ('1789', '程凯', 'workshop', 6, '111111'),
                 ('1614', '秦淑芳', 'workshop', 6, '111111'), ('1996', '温慧', 'workshop', 6, '111111'),
                 ('1463', '徐海天', 'workshop', 11, '111111'), ('1535', '范爱军', 'workshop', 11, '111111'),
                 ('1884', '李佳', 'workshop', 11, '111111'), ('0001', '张建志', 'workshop', 11, '111111'),
                 ('6554', '张利兵', 'workshop', 9, '111111'), ('0311', '李志冬', 'workshop', 7, '111111'),
                 ('1745', '吕彬', 'workshop', 10, '111111'), ('1720', '李奎', 'workshop', 11, '111111'),
                 ('1607', '赵新宇', 'workshop', 7, '111111'), ('6287', '陈平', 'workshop', 18, '111111')]
        for i in list1:
            args = {}
            card_id, name, emp_type, ws_id, password = i
            partner = self.env['res.partner'].sudo().create({'name': name})
            if ws_id != 0:
                args.update({'cr_emp_ws_id': ws_id})
            args.update({'login': card_id, 'partner_id': partner.id, 'cr_emp_type': emp_type, 'card_id': card_id,
                         'password': '111111'})
            self.env['res.users'].sudo().create(args)

